package com.sw.small_world.window;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;


/**
 * Created by HuaChing90403 on 2016/3/24.
 */
public class SelectImgMethodPopWindow extends PopupWindow {

    private View v;
    private View.OnClickListener itemOnClick;

    private TextView tvCamera;
    private TextView tvImageAlbum;
    private TextView tvCancel;
    private RelativeLayout bg;


    public SelectImgMethodPopWindow(Activity context, View.OnClickListener itemOnClick){
        super(context);
        this.itemOnClick = itemOnClick;
        v = View.inflate(context, R.layout.view_pop_select_img_method,null);
        initView();
        initPopWindow();
    }

    private void initPopWindow() {

        // 设置SelectPicPopupWindow的View
        this.setContentView(v);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的宽
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00FFFFFF);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);



    }

    private void initView() {


        //给照相机按钮和选择本地图库的按钮添加上activity的点击事件
        this.tvCamera = (TextView) v.findViewById(R.id.tv_select_img_take_photo);
        this.tvCamera.setOnClickListener(itemOnClick);
        this.tvImageAlbum = (TextView) v.findViewById(R.id.tv_select_img_album);
        this.tvImageAlbum.setOnClickListener(itemOnClick);

        bg = (RelativeLayout) v.findViewById(R.id.rl_bg);

        this.tvCancel = (TextView) v.findViewById(R.id.tv_select_img_cancel);
        //给取消按钮添加点击事件,点击则关闭当前泡泡窗口

        this.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        //给当前的泡泡窗口的布局设置一个触摸事件, 如果触摸在外面则关闭窗口

        this.v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = v.findViewById(R.id.ll_select_img).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
